package service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import model.Tpermission;
import model.Trolepermission;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pageModel.Menu;
import service.MenuServiceI;
import dao.MenuDaoI;
import dao.PermissionDaoI;
import dao.RolePermissionDaoI;
@Service("menuService")
public class MenuserviceImpl implements MenuServiceI {

private MenuDaoI menuDao;
private RolePermissionDaoI rolePermissionDao;
private PermissionDaoI permissionDao;
public MenuDaoI getMenuDao() {
	return menuDao;
}
@Autowired
public void setMenuDao(MenuDaoI menuDao) {
	this.menuDao = menuDao;
}

public RolePermissionDaoI getRolePermissionDao() {
	return rolePermissionDao;
}
@Autowired
public void setRolePermissionDao(RolePermissionDaoI rolePermissionDao) {
	this.rolePermissionDao = rolePermissionDao;
}

public PermissionDaoI getPermissionDao() {
	return permissionDao;
}
@Autowired
public void setPermissionDao(PermissionDaoI permissionDao) {
	this.permissionDao = permissionDao;
}

@Override
public List<Menu> getTree(String id) {
	//String flag="";
	List<Menu> lm=new ArrayList<Menu>();
	String hql="from Trolepermission t where t.trole.id=:id";
	Map<String,Object>params=new HashMap<String,Object>();
	params.put("id",id);
	List<Trolepermission> l = rolePermissionDao.find(hql,params);
	for(Trolepermission t : l){
		String hql1=null;
		if(t.getTpermission().getId()==null){
			//查询所有节点
			hql1="from Tpermission t where t.type='1' and t.tpermission is null";
			//flag="NotNUll";
		}else{
			//异步查询当前id下的子节点
			hql1="from Tpermission t where t.type='1' and t.id = '"+t.getTpermission().getId()+"'";
		} 
		List<Tpermission> lt=permissionDao.find(hql1);
		if(lt!=null&&lt.size()>0){
			for(Tpermission tp: lt){
			Menu m =new Menu();
			BeanUtils.copyProperties(tp, m);
			if(tp.getTpermission()!=null){
				m.setPid(tp.getTpermission().getId());
			}
			m.setText(tp.getText());
			m.setIconCls(tp.getIconCls());
			/*if(tp.getTpermissions()!=null&&!tp.getTpermissions().isEmpty()){
				m.setState("closed");
			}*/
			Map<String,Object> attributes=new HashMap<String,Object>();
			attributes.put("url",tp.getUrl());
			m.setAttributes(attributes);
			/*if(tp.getTrolepermissions()!=null&&!tp.getTrolepermissions().isEmpty()){
				p.setState("closed");//节点以文件形式体现
			}else{
				p.setState("open");//节点以文件夹形式体现
			}*/
			lm.add(m);
			}
		}
	}
	return lm;
}
@Override
public List<Menu> getAllTree(String id) {
	//String flag="";
		List<Menu> lm=new ArrayList<Menu>();
		String hql="from Trolepermission t where t.trole.id=:id";
		Map<String,Object>params=new HashMap<String,Object>();
		params.put("id",id);
		List<Trolepermission> l = rolePermissionDao.find(hql,params);
		for(Trolepermission t : l){
			String hql1=null;
			if(t.getTpermission().getId()==null){
				//查询所有节点
				hql1="from Tpermission t where t.tpermission is null";
				//flag="NotNUll";
			}else{
				//异步查询当前id下的子节点
				hql1="from Tpermission t where  t.id = '"+t.getTpermission().getId()+"'";
			} 
			List<Tpermission> lt=permissionDao.find(hql1);
			if(lt!=null&&lt.size()>0){
				for(Tpermission tp: lt){
				Menu m =new Menu();
				BeanUtils.copyProperties(tp, m);
				if(tp.getTpermission()!=null){
					m.setPid(tp.getTpermission().getId());
				}
				m.setText(tp.getText());
				/*if(tp.getTpermissions()!=null&&!tp.getTpermissions().isEmpty()){
					m.setState("closed");
				}*/
				/*Map<String,Object> attributes=new HashMap<String,Object>();
				attributes.put("url",tp.getUrl());
				m.setAttributes(attributes);*/
				/*if(tp.getTrolepermissions()!=null&&!tp.getTrolepermissions().isEmpty()){
					p.setState("closed");//节点以文件形式体现
				}else{
					p.setState("open");//节点以文件夹形式体现
				}*/
				lm.add(m);
				}
			}
	}
	return lm;
}

}
